//
//  YXYScrollImageTableViewCell.m
//  Topit
//
//  Created by dllo on 15/12/8.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYScrollImageTableViewCell.h"
#import "YXYZoomScrollCollectionViewCell.h"

@implementation YXYScrollImageTableViewCell

- (void)dealloc {
    [_collectionView release];
    [_dataSource release];
    [_correlation release];
    [_UrlArray release];
    [_zoomImageModel release];
    _delegate = nil;
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.correlation = [[UILabel alloc] initWithFrame:CGRectZero];
        _correlation.font = [UIFont systemFontOfSize:10];
        _correlation.textColor = [UIColor grayColor];
        [self addSubview:_correlation];
        [_correlation release];
        
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        //隐藏滚动条
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.collectionView];
        [self.collectionView registerClass:[YXYZoomScrollCollectionViewCell class] forCellWithReuseIdentifier:@"YXYZoomScrollCollectionViewCellIdentifier"];
        [_flowLayout release];
        [_collectionView release];
    }
    return self;
}

- (void)setDataSource:(NSMutableArray *)dataSource {
    if (_dataSource != dataSource) {
        [_dataSource release];
        _dataSource = [dataSource retain];
    }
}

- (void)setUrlArray:(NSMutableArray *)UrlArray {
    if (_UrlArray != UrlArray) {
        [_UrlArray release];
        _UrlArray = [UrlArray retain];
    }
}


-(void)layoutSubviews {
    [super layoutSubviews];
    _correlation.frame = CGRectMake(5, 5, self.width, 10);
    _flowLayout.itemSize = CGSizeMake(self.width / (_dataSource.count - 3), self.height - 20);
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.minimumLineSpacing = 0;
    _collectionView.frame = CGRectMake(0, 15, self.width, self.height - 20);
    [_collectionView nightMode];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYZoomScrollCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYZoomScrollCollectionViewCellIdentifier" forIndexPath:indexPath];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:_dataSource[indexPath.item]]];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.delegate scrollImageSkip:_UrlArray[indexPath.item]];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
